/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.zuq.geocode;

import com.gisgraphy.domain.geoloc.service.geoloc.street.StreetType;
import java.io.DataInputStream;
import java.io.IOException;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author zuq
 */
public class GisgraphyGeocodeService implements ReverseGeocodeService {

    public static final String GISGRAPHY_URL = "http://localhost:8080/street/";
    private static final String STREETTYPE_PARAMETER = "streettype";
    private static final String LAT_PARAMETER = "lat";
    private static final String LONG_PARAMETER = "lng";
    private static final String RADIUS_PARAMETER = "radius";
    private static final String ONEWAY_PARAMETER = "oneway";
    private static final String NAME_PARAMETER = "name";
    private static final String STREET_SEARCH_MODE_PARAMETER = "mode";

    public List<NearStreet> findNearestStreets(double lat, double lng, double radius) {
        return findNearestStreets(lat, lng, radius, null);
    }

    public List<NearStreet> findNearestStreets(double lat, double lng,
            double radius, StreetType type) {
        return findNearestStreets(lat, lng, radius, type, null, null);
    }

    public List<NearStreet> findNearestStreets(double lat, double lng,
            double radius, StreetType type, String name, Boolean oneWay) {

        StringBuilder query = new StringBuilder(GISGRAPHY_URL).append("?").
                append(LAT_PARAMETER).
                append("=").append(Double.toString(lat)).
                append("&").append(LONG_PARAMETER).
                append("=").append(Double.toString(lng)).
                append("&").append(RADIUS_PARAMETER).
                append("=").append(Double.toString(radius));

        appendNotNull(query, type, STREETTYPE_PARAMETER);
        appendNotNull(query, name, NAME_PARAMETER);
        appendNotNull(query, type, ONEWAY_PARAMETER);

        try {


            URLConnection conn = new URL(query.toString()).openConnection();
            String result = readResult(conn);

            List<NearStreet> streets = GisgraphyXMLParser.parseString(result);

            return streets;

        } catch (IOException ex) {
            ex.printStackTrace();
        }

        return new ArrayList<NearStreet>();
    }

    private void appendNotNull(StringBuilder query, Object obj, String param) {
        if (obj != null) {
            query.append("&").
                    append(param).
                    append("=").append(obj);
        }
    }

    /**
     * Connects to the URLConnection target and read the result in a String.
     *
     * @param conn the URLConnection to read
     * @return a String with the result
     */
    private String readResult(URLConnection conn) {

        DataInputStream dis = null;
        StringBuilder result = new StringBuilder();

        try {
            dis = new DataInputStream(conn.getInputStream());

            String inputLine;

            while ((inputLine = dis.readLine()) != null) {
                result.append(inputLine);
            }

        } catch (IOException ex) {
            ex.printStackTrace();
        } finally {
            try {
                dis.close();
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }

        return result.toString();
    }
}
